package com.ekuaitu.kuaitustaff.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.ekuaitu.kuaitustaff.R;
import com.ekuaitu.kuaitustaff.application.MyApplication;

import java.util.List;

/**
 * com.bm.falvzixun.adapter.GridViewAddImgAdpter
 *
 * @author caojing on 2015/12/24.
 *         添加上传图片适配器
 */
public class GridViewAdpter extends BaseAdapter {
    private List<String> datas;
    private Context context;
    private LayoutInflater inflater;

    public GridViewAdpter(List<String> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    /**
     * 让GridView中的数据数目加1最后一个显示+号
     *
     * @return 返回GridView中的数量
     */
    @Override
    public int getCount() {
        int count = datas == null ? 0 : datas.size();
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void notifyDataSetChanged(List<String> datas) {
        this.datas = datas;
        this.notifyDataSetChanged();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_comment_gv_pic, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (datas != null && position <= datas.size()) {
            Glide.with(context)
                    .load(MyApplication.imageUrl + datas.get(position))
                    .priority(Priority.HIGH)
                    .centerCrop()
                    .into(viewHolder.ivimage);
        }

        return convertView;

    }

    public class ViewHolder {
        public final ImageView ivimage;

        public ViewHolder(View root) {
            ivimage = (ImageView) root.findViewById(R.id.iv_image);

        }
    }
}
